/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$(function () {
    $(".info_confirm").click(function () {
        var p = $("#sel_pro").val();
        if (p === "0" || p === "") {
            alert("请选择省份");
            return;
        }
        var c = $("#sel_city").val();
        if (c === "0" || c === "") {
            alert("请选择城市");
            return;
        }
        var a = $("#sel_area").val();
        if (a === "0" || a === "") {
            alert("请选择区域");
            return;
        }
        var address = $("#address").val();
        if (address == "") {
            alert("请填写详细地址");
            return;
        }
        var zipcode = $("#zipcode").val();
        var comname = $("#comname").val();
        if (comname == "") {
            alert("请填写收货人姓名");
            return;
        }
        var namemobile = $("#namemobile").val();
        if (namemobile == "") {
            alert("请填写收货人手机号码");
            return;
        }
        if (!isMoible(namemobile)) {
            alert("手机号格式不正确，请重新填写");
            return;
        }
        var userId = $("#hiddenUserId").val();
        var id = $("#hiddenId").val();
        $.ajax({
            type: "post",
            url: "/updateUserAddress.do",
            timeout: 1000,
            data: {name: $.trim(comname), mobile: $.trim(namemobile), address: $.trim(address), zipCode: $.trim(zipcode), userId: userId, id: id, p: p, c: c, a: a},
            dataType: "text",
            cache: false,
            success: function (data) {
                if (data === "addsuccess") {
                    alert("添加成功");
                    location.reload();
                    $("#address").val("");
                    $("#zipcode").val("");
                    $("#comname").val("");
                    $("#namemobile").val("");
                } else if (data === "success") {
                    alert("更改成功");
                    location.reload();
                    $("#address").val("");
                    $("#zipcode").val("");
                    $("#comname").val("");
                    $("#namemobile").val("");
                } else if (data === "addfalse") {
                    alert("添加失败");
                } else if (data === "false") {
                    alert("更改失败");
                } else {
                    alert("系统错误，操作失败");
                }
            }
        });
    });
});
function getAddressById(id) {
    $("#hiddenId").val(id);
    $.ajax({
        type: "post",
        url: "/getAddressById.do",
        timeout: 1000,
        data: {id: id},
        dataType: "json",
        cache: false,
        success: function (data) {
            $("#sel_pro").val(data.province);
            $("#address").val(data.address);
            $("#zipcode").val(data.zipCode);
            $("#comname").val(data.name);
            $("#namemobile").val(data.mobile);
            $(".info_confirm").html("编辑地址");
            getCityLists(data.city, data.area);
        }
    });
}
function getCityLists(cid, countid) {
    var pid = $.trim($("#sel_pro").val());
    $.ajax({
        type: "get",
        url: "/userInfo/getCityList.do",
        timeout: 1000,
        data: {id: pid},
        dataType: "json",
        cache: false,
        success: function (data) {
            var strHtml = "<option value=\"0\" selected>选择城市</option>";
            var citylist = data;
            if (citylist.length > 1) {
                for (var i = 0; i < citylist.length; i++) {
                    strHtml += "<option value='" + citylist[i].cityid + "'>" + citylist[i].city + "</option>";
                }
            }
            $("#sel_city").html(strHtml);
            if (cid === undefined) {
                cid = 0;
            }
            $("#sel_city").val(cid);
            if (countid === undefined) {
                countid = 0;
            }
            getAreaLists(countid);
        }
    });

}
function getAreaLists(countid) {
    $.ajax({
        type: "get",
        url: "/userInfo/getAreaList.do",
        timeout: 1000,
        data: {id: $.trim($("#sel_city").val())},
        dataType: "json",
        cache: false,
        success: function (data) {
            var strHtml = "<option value=\"0\" selected>选择县区</option>";
            var arealist = data;
            if (arealist.length > 1) {
                for (var i = 0; i < arealist.length; i++) {
                    strHtml += "<option value='" + arealist[i].areaid + "'>" + arealist[i].area + "</option>";
                }
            }
            $("#sel_area").html(strHtml);
            $("#sel_area").val(countid);
        }
    });
}
function updateStateById(id) {
    var userId = $("#hiddenUserId").val();
    if (confirm('确定要设置为默认吗?')) {
        $.ajax({
            type: "post",
            url: "/updateAddressState.do",
            timeout: 1000,
            data: {id: id, userId: userId},
            dataType: "text",
            cache: false,
            success: function (data) {
                if (data == "success") {
                    alert("更改成功");
                    location.reload();
                } else {
                    alert("更改失败");
                }
            }
        });
    }

}
function deleteAddress(id) {
    if (confirm('确定要删除该地址吗?')) {
        $.ajax({
            type: "post",
            url: "/deleteAddress.do",
            timeout: 1000,
            data: {id: id},
            dataType: "text",
            cache: false,
            success: function (data) {
                if (data == "success") {
                    alert("删除成功");
                    location.reload();
                } else if (data == "dfalse") {
                    alert("默认地址无法删除");
                } else {
                    alert("删除失败");
                }
            }
        });
    }

}

function isMoible(mobile) {
    if (!(/^1[3|4|5|7|8][0-9]\d{8}$/.test(mobile))) {
        return false;
    }
    return true;
}